package array;

public class ApplyGravity {

    public void applyGravity(int[][] board) {
       if (board == null || board.length == 0)
           return;

       int rows = board.length;
       int cols = board[0].length;

       // 遍历每一列
        for (int col = 0; col < cols; col++) {

            // 初始化快慢指针
            int slow = rows - 1;
            int fast = rows - 1;

            while (fast >= 0) {
                if (board[fast][col] != 0) {
                    // 将非零元素移动到 slow 位置
                    board[slow][col] = board[fast][col];
                    slow--;
                }
                fast--;
            }
            // 填充剩余位置为零
            for (int i = 0; i <= slow; i++) {
                board[i][col] = 0;
            }
        }
    }
}
